package com.stone.back.dish.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.dish.entity.BfDish;
import com.stone.back.dish.entity.BfDishType;
import com.stone.back.dish.service.IDishService;
import com.stone.back.user.entity.BfUser;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;

/**
 * 菜品管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/backDishCtl")
public class DishController extends BaseController {
	@Autowired
	private IDishService dishService;
	
	/**
	 * 查询菜品分页信息
	 * @param pageNumber	页数
	 * @param dishChName	中文菜名
	 * @param dishEnName	英文菜名
	 * @param priceMax		单价
	 * @param priceMin		单价	
	 * @param startDate		创建日期
	 * @param endDate		创建日期
	 * @return
	 */
	@RequestMapping("/select")
    public @ResponseBody Page selectDishPage(String pageNumber, String dishChName, String dishEnName, String priceMin, String priceMax, 
    		String startDate, String endDate, String dishTypeId, HttpServletRequest request){
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "1";
		}
		Page page = dishService.selectDishPage(Integer.parseInt(pageNumber), dishChName, dishEnName, priceMin, priceMax, startDate, endDate, dishTypeId);
		return page;
	}
	
	/**
	 * 查询菜品分类列表
	 * @return
	 */
	@RequestMapping("/selectDishType")
    public @ResponseBody List<BfDishType> selectDishType(HttpServletRequest request) {
		List<BfDishType> list = this.dishService.selectDishType();
		return list;
	}
	
	/**
	 * 增加菜品
	 * @param dish	菜品信息
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/add")
    public ModelAndView addDish(BfDish dish, @RequestParam MultipartFile dishImage, HttpServletRequest request) throws IOException {
		String realPath = request.getSession().getServletContext().getRealPath("/upload");
		String fileName = dishImage.getOriginalFilename();
		String fileExtensionName = fileName.substring(fileName.lastIndexOf("."));
		String dishImagePath = realPath + File.separator + System.currentTimeMillis() + fileExtensionName;
		FileUtils.copyInputStreamToFile(dishImage.getInputStream(), new File(dishImagePath));
		ModelAndView mv = new ModelAndView("/background/dish/dishlist");
		dish.setCreatedate(new Date());
		BfUser currentUser = this.getBackCurrentUser(request);
		dish.setCreateuserid(currentUser.getUserid());
		dish.setPhoto("/upload/" + dishImagePath.substring(dishImagePath.lastIndexOf(File.separator) + 1));
		dishService.addDish(dish);
		return mv;
	}
	
	/**
	 * 获取菜品信息去修改
	 * @param dish	菜品信息
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/toUpdate")
    public ModelAndView toUpdateDish(int dishId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/dish/dishupdate");
		BfDish dish = dishService.selectDishDetail(dishId);
		mv.addObject("dish", dish);
		return mv;
	}
	
	/**
	 * 修改菜品
	 * @param dish	菜品信息
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/update")
    public ModelAndView updateDish(BfDish dish, @RequestParam MultipartFile dishImage, HttpServletRequest request) throws IOException{
		ModelAndView mv = new ModelAndView("/background/dish/dishlist");
		dish.setCreatedate(new Date());
		BfUser currentUser = this.getBackCurrentUser(request);
		dish.setCreateuserid(currentUser.getUserid());
		if(0 != dishImage.getSize()){
			//修改图片
			String realPath = request.getSession().getServletContext().getRealPath("/upload");
			String fileName = dishImage.getOriginalFilename();
			String fileExtensionName = fileName.substring(fileName.lastIndexOf("."));
			String dishImagePath = realPath + File.separator + System.currentTimeMillis() + fileExtensionName;
			FileUtils.copyInputStreamToFile(dishImage.getInputStream(), new File(dishImagePath));
			String oldFilePath = dish.getPhoto();
			//删除旧图片
			if(StringUtils.isNotBlank(oldFilePath)) {
				oldFilePath = request.getSession().getServletContext().getRealPath(oldFilePath);
				File file = new File(oldFilePath);
				file.delete();
			}
			dish.setPhoto("/upload/" + dishImagePath.substring(dishImagePath.lastIndexOf(File.separator) + 1));
		}
		dishService.updateDish(dish);
		return mv;
	}
	
	/**
	 * 查询菜品详情
	 * @param dishId	菜品Id
	 * @return
	 */
	@RequestMapping("/detail")
    public ModelAndView selectDishDetail(int dishId, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/background/dish/dishdetail");
		BfDish dish = dishService.selectDishDetail(dishId);
		mv.addObject("dish", dish);
		return mv;
	}
	
	/**
	 * 删除菜品
	 * @param dishId	菜品Id
	 * @return
	 */
	@RequestMapping("/delete")
    public @ResponseBody boolean deleteDish(int dishId, HttpServletRequest request){
		BfDish dish = dishService.selectDishDetail(dishId);
		//删除旧图片
		if(StringUtils.isNotBlank(dish.getPhoto())) {
			String fileRealPath = request.getSession().getServletContext().getRealPath(dish.getPhoto());
			File file = new File(fileRealPath);
			file.delete();
		}
		dishService.deleteDish(dishId);
		return true;
	}
}
